package com.talent.jiwen_teacher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.http.result.OrderListResult;
import com.talent.jiwen_teacher.ui.widgets.CourseInfoNewView;
import com.talent.jiwen_teacher.util.MyToast;
import com.talent.jiwen_teacher.util.SPConstant;
import com.talent.jiwen_teacher.util.SPUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;

public class WaitSureAdapter extends RecyclerView.Adapter<WaitSureAdapter.WaitSureViewHoler>{
    private Context context;
    private List<OrderListResult.Order> dataList;
    private List<WaitSureViewHoler> holderList = new ArrayList<>();

    public WaitSureAdapter(Context context, List<OrderListResult.Order> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public WaitSureViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(context).inflate(R.layout.item_wait_sure , parent , false);
        View view = LayoutInflater.from(context).inflate(R.layout.item_wait_sure_new , parent , false);
        return new WaitSureViewHoler(view);
    }

    @Override
    public void onBindViewHolder(WaitSureViewHoler holder, final int position) {
        holder.setDataPosition(position);
        if (!(holderList.contains(holder))){
            holderList.add(holder);
        }

        final OrderListResult.Order data = dataList.get(position);

        if (data.getAnswerType() == 1){
            //即时开课
//            holder.typeTv.setBackgroundResource(R.drawable.bg_corner_green);
//            holder.typeTv.setText(context.getString(R.string.immediately));
//            holder.timeTv.setTextColor(ContextCompat.getColor(context, R.color.text_gray_3));
//            holder.timeTv.setText(R.string.direct);
            holder.cancelTv.setVisibility(View.GONE);
        }else{
            //预约
//            holder.typeTv.setBackgroundResource(R.drawable.bg_corner_orange);
//            holder.typeTv.setText(context.getString(R.string.order_time));
//            holder.timeTv.setTextColor(ContextCompat.getColor(context , R.color.color_FA7D31));
//            SimpleDateFormat df = new SimpleDateFormat("MM月dd日 HH点mm分");
//            holder.timeTv.setText(df.format(new Date(data.getAnswerTime())));
            holder.cancelTv.setVisibility(View.VISIBLE);
        }

//        holder.gradeTv.setText(Constant.gradeMap.get(data.getGradeLevel()));
        holder.courseInfoView.bindData(data);

        holder.waitTimeTv.setText(data.getChangeTime());

        holder.cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                teacherCancelOrder(data.getOrderId() , position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    /**只刷新相关数据
     *
     */
    public void notifyData(){
        if(dataList.size() > holderList.size() || dataList.size() == holderList.size()){
            for (int i = 0 ; i < holderList.size() ; i ++){
                holderList.get(i).waitTimeTv.setText(dataList.get(holderList.get(i).position).getChangeTime());
            }
        }else{
            holderList.clear();
            notifyDataSetChanged();
        }
    }

    /**取消订单
     *
     * @param orderId
     */
    private void teacherCancelOrder(String orderId , final int postion){
        Map<String, String> map = new HashMap<>();
        map.put("teacherId", SPUtil.getUserId());
        map.put("orderId", orderId);

        Observable observable = ApiFactory.getEducationApiSingleton().teacherCancelOrder(SPUtil.stringOut(SPConstant.TOKEN), map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber(context) {
            @Override
            protected void _onNext(Object o) {
                MyToast.show(context , "取消成功");
                dataList.remove(postion);
                notifyDataSetChanged();
            }

            @Override
            protected void _onError(String message) {
                MyToast.show(context , message);
            }
        } , true);
    }

    class WaitSureViewHoler extends RecyclerView.ViewHolder{
//        TextView typeTv;
//        TextView timeTv;
//        TextView gradeTv;
//        CourseInfoWaitView courseInfoView;
        CourseInfoNewView courseInfoView;
        TextView waitTimeTv;
        TextView cancelTv;

        private int position;

        public void setDataPosition(int position){
            this.position = position;
        }

        public WaitSureViewHoler(View itemView) {
            super(itemView);
//            typeTv = itemView.findViewById(R.id.typeTv);
//            timeTv = itemView.findViewById(R.id.timeTv);
//            gradeTv = itemView.findViewById(R.id.gradeTv);
            courseInfoView = itemView.findViewById(R.id.courseInfoView);
            waitTimeTv = itemView.findViewById(R.id.waitTimeTv);
            cancelTv = itemView.findViewById(R.id.cancelTv);
        }
    }
}
